/* Copyright 2001,2002,2003 NAH6
 * All Rights Reserved
 *
 * Parts Copyright DoD, Parts Copyright Starium
 *
 */
#include "end_correct_adapt.h"
#include "main.h"
#include <assert.h>

/**************************************************************************
*
* ROUTINE
*               EndCorrectAdapt
*
* FUNCTION
*               End correct the convulution sum
* SYNOPSIS
*               EndCorrectAdapt(conv, Exc, Exc_len, pc_imp, len_trunc)
*
*   formal
*
*                       data    I/O
*       name            type    type    function
*       -------------------------------------------------------------------
*       conv            fxpt_16 i/o     Truncated convolved impulse response
*       Exc             fxpt_16  i      Excitation vector
*       Exc_len         int      i      Length of excitation vector
*       pc_imp          fxpt_16  i      Impulse response of PCs
*       len_trunc       int      i      Length of truncated impulse response
*
**************************************************************************
*          End correct the convolution sum on subsequent delays:
*
*          y     =  0
*           0, t
*          y     =  y        + ex  * h   where i = 1, ..., L points
*           i, m     i-1, m-1   -m    i  and   m = t+1, ..., tmax lags
*
**************************************************************************/

void EndCorrectAdapt(
  fxpt_32       conv[],                         /* 18.13 format */
  fxpt_16       Exc[],                          /* 15.0 format */
  fxpt_16       pc_imp[]                        /* 2.13 format */
  )
{
    int i;
	fxpt_16 *pp,  e0;
	fxpt_32 *pc;

/*
        for(i = SF_LEN - 1; i >= LEN_TRUNC_H; i--) {
          conv[i] = conv[i-1];
        }

        for (i = LEN_TRUNC_H - 1; i >= 1; i--) {
          c =  Exc[0] * pc_imp[i];
          c += conv[i-1];
          conv[i] = c;
        }
        c = Exc[0] * pc_imp[0];
        conv[0] = c;
*/

	for(i = SF_LEN - LEN_TRUNC_H, pc= conv+SF_LEN-1; i > 0; --i, --pc) {
		pc[0]= pc[-1]; // conv[i] = conv[i-1];
	}
	e0= Exc[0];
 	for (i = LEN_TRUNC_H - 1, pc= conv+LEN_TRUNC_H-1, pp= pc_imp+LEN_TRUNC_H-1; i>0; --i, --pc, --pp) {
		pc[0] = pc[-1] + e0 * *pp;
	}
	pc[0] = e0 * *pp;
}
